<!-- prettier-ignore -->
<template>
  <el-dialog :visible.sync="dialogVisible" title="创建任务" width="30%">
    <el-form label-width="100px">
      <el-form-item label="任务名称：">
        <el-input v-model="taskParams.name"></el-input>
      </el-form-item>
      <el-form-item label="创建人：">
        <el-input v-model="taskParams.user" disabled></el-input>
      </el-form-item>
      <el-form-item label="所属单位：">
        <el-input v-model="taskParams.unit" disabled></el-input>
      </el-form-item>
      <el-form-item label="数据范围：">
        <!--<el-cascader v-model="dataSource" :props="cityProps" clearable style="width: 100%">-->
        <!--</el-cascader>-->
        <district-select v-model="dataSource"></district-select>
      </el-form-item>
      <el-form-item label="选择文件：">
        <div style="display: flex;justify-content: space-between">
          <el-upload
            :before-upload="beforeUpload"
            action=""
            class="upload-demo">
            <el-button style="margin-right:5px" type="primary">点击上传</el-button>
          </el-upload>
          <el-input v-model="fileName"></el-input>
        </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="startParticiple">开始分词</el-button>
    </span>
  </el-dialog>
</template>

<script>
import axios from 'axios'
import districtSelect from '@/views/oneCodeAccess/districtSelect.vue';
export default {
  name: 'taskCreate',
  components:{districtSelect},
  data(){
    return {
      fileName:'',
      dataSource:[],
      taskParams:{
        name:'',
        source:'',
        user:'李建',
        unit:'省政务服务和数据管理局',
        type:1
      },
      cityProps: {
        value: "name",
        label: "name",
        checkStrictly: true,
        children: "districts",
        lazy: true,
        lazyLoad: this.getCityProps,
      },
    }
  },
  props:{
    createShow:{
      type:Boolean,
      default:true
    }
  },
  methods:{
    beforeUpload(file){
      this.fileName = file.name
      return false
    },
    // 创建分词任务
    async startParticiple(){
      this.taskParams.source = this.dataSource.join(' / ')
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      const res = await axios.post(`${window.baseUrl}/address/task/create`,this.taskParams)
      if(res.status === 200){
        loading.close()
        this.$message.success('任务创建成功')
        this.$parent.updateList()
        this.dialogVisible = false
      } else {
        this.$message.error('任务创建失败')
        loading.close()
      }
    },
    // 获取城市信息
    async getCityProps(node, resolve) {
      let nodes = [];
      let keywords = ''
      if (node.level === 0) {
        keywords = '黄冈市'
      } else {
        keywords = node.value
      }
      let res = await axios.get(
        "https://restapi.amap.com/v3/config/district",
        {
          params: {
            keywords,
            subdistrict: 2,
            key: "4bb9d93c9b35ffdc9a5177d230394f23",
          },
        }
      );
      res.data.districts[0].districts.forEach((item) => {
        const leaf = !item.districts.length;
        nodes.push({ ...item,leaf });
      });
      resolve(nodes)
    },
  },
  computed:{
    dialogVisible:{
      get(){
        return this.createShow
      },
      set(val){
        this.$emit('update:createShow',val)
      }
    },
  }
};
</script>

<style lang="less">
.create-box {
}
</style>
